package com.ccdc.lbcmsystem.controller;


import com.ccdc.lbcmsystem.mapper.RoleMapper;
import com.ccdc.lbcmsystem.mapper.UserMapper;
import com.ccdc.lbcmsystem.pojo.Role;
import com.ccdc.lbcmsystem.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



@Controller
public class AdminController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @RequestMapping("/adminList")
    public String adminList(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                            @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<User> users = userMapper.queryUserList();
        PageInfo<User> usersPageInfo = new PageInfo<>(users);
        model.addAttribute("usersPageInfo", usersPageInfo);
        model.addAttribute("users",users);
        return "admin/adminList";
    }

    @RequestMapping("/searchUser")
    public String searchUser(String userName, Model model, @RequestParam(defaultValue = "1") Integer startPage,
                             @RequestParam(defaultValue = "10") Integer pageSize, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<User> users = userMapper.queryListUserByUserName(userName);
        PageInfo<User> usersPageInfo = new PageInfo<>(users);
        session.setAttribute("userName",userName);
        model.addAttribute("usersPageInfo", usersPageInfo);
        model.addAttribute("users",users);
        return "admin/adminSearchList";
    }
    @RequestMapping("/toAdminAdd")
    public String toAdminAdd(){
        return "admin/adminAdd";
    }
    @RequestMapping("/adminAdd")
    @ResponseBody
    public String adminAdd(String name, String username, String password,  String email, String tel, String role) {
        String msg = "";
        User user1 = userMapper.queryUserByUserName(username.replace(" ",""));
        if (StringUtils.isBlank(username) || username.equals("")){
            msg = "请输入用户名";
            return msg;
        }
        if (StringUtils.isBlank(name) || name.equals("")){
            msg = "请输入姓名";
            return msg;
        }
        if (StringUtils.isBlank(password) || password.equals("")){
            msg = "请输入密码";
            return msg;
        }
        if (StringUtils.isBlank(email) || email.equals("")){
            msg = "请输入邮箱";
            return msg;
        }
        if (StringUtils.isBlank(tel) || tel.equals("")){
            msg = "请输入电话号码";
            return msg;
        }
        if (user1 != null && !username.equals("")){
            msg = "用户名已存在,请重新输入";
            return msg;
        }
        String regEx4 = "^[A-Za-z0-9]+$";
        if (!username.matches(regEx4)){
            msg = "用户名只能由字母和数字组成";
            return msg;
        }
        String regEx3 = "^[\u4e00-\u9fa5]{0,6}$";
        if (!name.matches(regEx3)){
            msg = "姓名只能由汉字组成";
            return msg;
        }
        String regEx2 = "^[A-Za-z0-9]+$";
        if (!password.matches(regEx2)){
            msg = "密码只能由数字和字母组成";
            return msg;
        }
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(email);
        if (!m.matches() && !email.equals("")){
            msg = "邮箱格式错误,请重新输入";
            return msg;
        }
        String REG_EXP = "^([0-9]{3}-?[0-9]{8}|[0-9]{4}-?[0-9]{7})$";
        if (!tel.matches(REG_EXP) && !tel.equals("")){
            msg = "手机号码错误,请重新输入";
            return msg;
        }
        else {
            msg = "OK";
            User user = new User();
            user.setName(name.replace(" ",""));
            user.setUserName(username.replace(" ",""));
            user.setPassWord(password);
            user.setRole(role);
            user.setEmail(email);
            user.setTel(tel);
            SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime = new Date();
            String time = sft.format(createTime);
            user.setCreateTime(time);
            //管理员添加账号直接通过审核 状态码设置为 1
            String examineStatus = "1";
            user.setExamineStatus(examineStatus);
            userMapper.addUser(user);
            return msg;
        }
    }

    @RequestMapping("/toAdminEdit/{id}")
    public String toAdminEdit(@PathVariable("id") Integer id, Model model){
        User user = userMapper.queryUserById(id);
        List<Role> roles = roleMapper.queryAll();
        model.addAttribute("roles",roles);
        model.addAttribute("user",user);
        return "admin/adminEdit";
    }
    @RequestMapping("/adminEdit")
    @ResponseBody
    public String adminEdit(Integer id, String name, String passWord,
                             String email, String tel, String role){
        String msg = "";
        if (StringUtils.isBlank(name) || name.equals("")){
            msg = "请输入姓名";
            return msg;
        }
        if (StringUtils.isBlank(passWord) || passWord.equals("")){
            msg = "请输入密码";
            return msg;
        }
        if (StringUtils.isBlank(email) || email.equals("")){
            msg = "请输入邮箱";
            return msg;
        }
        if (StringUtils.isBlank(tel) || tel.equals("")){
            msg = "请输入电话号码";
            return msg;
        }
        String regEx3 = "^[\u4e00-\u9fa5]{0,6}$";
        if (!name.matches(regEx3)){
            msg = "姓名只能由汉字组成";
            return msg;
        }
        String regEx2 = "^[A-Za-z0-9]+$";
        if (!passWord.matches(regEx2)){
            msg = "密码只能由数字和字母组成";
            return msg;
        }
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(email);
        if (!m.matches() && !email.equals("")){
            msg = "邮箱格式错误,请重新输入";
            return msg;
        }
        String REG_EXP = "^([0-9]{3}-?[0-9]{8}|[0-9]{4}-?[0-9]{7})$";
        if (!tel.matches(REG_EXP) && !tel.equals("")){
            msg = "手机号码错误,请重新输入";
            return msg;
        }
        else {
            userMapper.updateUserById(id,name,passWord,email,tel,role);
            msg = "OK";
            return msg;
        }

    }

    @RequestMapping("/adminDelete")
    @ResponseBody
    public String adminDelete(Integer id, Model model){
        String msg = "";
        try {
            userMapper.delUserById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "删除失败";
            return msg;
        }
    }
    @RequestMapping("/enableUser")
    @ResponseBody
    public String enableUser( Integer id, Model model){

        String msg = "";
        try {
            userMapper.enableUserById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "启用失败";
            return msg;
        }
    }
    @RequestMapping("/disableUser")
    @ResponseBody
    public String disableUser( Integer id, Model model){

        String msg = "";
        try {
            userMapper.disableUserById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "禁用失败";
            return msg;
        }
    }
    //初始化密码
    @RequestMapping("/initializePwd")
    @ResponseBody
    public String initializePwd( Integer id, Model model){

        String msg = "";
        try {
            userMapper.initializePwdById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "删除失败";
            return msg;
        }
    }
}
